// 发送给后端，进行检测表单数据逻辑

import {
    toast,
    promptBox
} from 'nc-lightapp-front';
import checkPersonInfo from '../request/checkPersonInfo';
import checkDataRequired from './checkDataRequired';
import language from '../../../language';
import {saveValidate} from 'src/hrpub/common/utils/utils';

export default function (func_type = 2, pageId = 60071701) {
    let {
        // 存储了新增页面的模版
        addPageTemplate,
        // 人力资源组织信息
        humanResourceOrganizationValue,
        // 从哪个页面跳转过来
        pageFrom,
        form,
        imageData,
        selectedRows,
        cardTable,
        pageConfig,
        meta
    } = this.props;

    let allTableData = this.getAllTableData();

    let postData = {
        ...allTableData,
        CurrentPkPsndoc: addPageTemplate.Pk_psncl || null,
        InJobType: addPageTemplate.InJobType || 'hire',
        func_type: func_type,
        pk_org: humanResourceOrganizationValue.refpk,
        ui_state: pageFrom === 'add' ? 1 : 2,
        bd_psndoc: form.getAllFormValue('bd_psndoc'),
        hi_psnjob_h: form.getAllFormValue('hi_psnjob_h'),
        hi_psnorg_h: form.getAllFormValue('hi_psnorg_h'),
        image: imageData
    };

    let id = postData['bd_psndoc'].rows[0].values['id'];
    let idType = postData['bd_psndoc'].rows[0].values['idtype'];

    try {
        cardTable.setValByKeyAndIndex('hi_psndoc_cert', 0, 'id', {
            ...id
        });
        cardTable.setValByKeyAndIndex('hi_psndoc_cert', 0, 'idtype', {
            ...idType
        });
    } catch (e) {
        console.error(e);
    }

    setTimeout(async () => {
        let template = meta.getMeta();
        // const formLists = ['bd_psndoc', 'hi_psnjob_h', 'hi_psnorg_h'];
        // 验证公式
        await saveValidate(
            this.props,
            '60071020p',
            'bd_psndoc',
            [],
            'form',
            null,
            null
        );
        await saveValidate(
            this.props,
            '60071020p',
            'hi_psnjob_h',
            [],
            'form',
            null,
            null
        );
        await saveValidate(
            this.props,
            '60071020p',
            'hi_psnorg_h',
            [],
            'form',
            null,
            null
        );

        for (let tableId in allTableData) {
            await saveValidate(
                this.props,
                '60071020p',
                tableId,
                [],
                'grid',
                'cardTable',
                null
            );
        }


        // 只有通过了必填项检测才会继续执行
        checkDataRequired.call(this, pageId, Object.keys(allTableData)).then(() => {
            checkPersonInfo(postData)
                .then((res) => {
                    if (res.success) {
                        // 会有提示信息
                        if (res.data.errMsg) {
                            toast({
                                color: 'danger',
                                content: res.data.errMsg
                            });
                            if (res.data.newPsnCode) {
                                selectedRows[0].data.values['pk_psndoc.code'] = {
                                    display: res.data.newPsnCode,
                                    value: res.data.newPsnCode
                                };
                                form.setFormItemsValue('bd_psndoc', {
                                    code: {
                                        display: res.data.newPsnCode,
                                        value: res.data.newPsnCode
                                    }
                                });
                            }
                            if (res.data.newClerkCode) {
                                selectedRows[0].data.values['clerkcode'] = {
                                    display: res.data.newClerkCode,
                                    value: res.data.newClerkCode
                                };
                                form.setFormItemsValue('hi_psnjob_h', {
                                    clerkcode: {
                                        display: res.data.newPsnCode,
                                        value: res.data.newPsnCode
                                    }
                                });
                            }
                            return;
                        }
                        if (res.data.confirmMsg && res.data.confirmMsg.length > 0) {
                            promptBox({
                                color: "warning",
                                title: language['hi6007-000008'],/* 国际化处理： 提示*/
                                content: (
                                    <div dangerouslySetInnerHTML={{
                                        __html: res.data.confirmMsg.join('<br/>')
                                    }}></div>
                                ),
                                beSureBtnClick: () => {
                                    this.saveNewPersonStep2(postData);
                                }
                            });
                        } else {
                            this.saveNewPersonStep2(postData);
                        }

                        if (res.data.worningMsg && res.data.worningMsg.length > 0) {
                            toast({
                                color: 'warning',
                                content: res.data.worningMsg.join(';')
                            });
                        }
                    }

                });
        })
    }, 1);
}
